<?php
class list_matiere_premiere
{
	private	$tabActor;
	private	$currentPos;
	private $nbElement;

	function	__construct()
	{
		$this->currentPos = 0;
		$this->nbElement = 0;
		$this->tabActor = array();
	}

	public	function	setCollActorbyMovieId($idM)
	{
		$req = 'SELECT ACTOR.IDA,FIRSTNAME,LASTNAME FROM ACTOR,PLAY WHERE ACTOR.IDA = PLAY.IDA AND IDM = ' . $idM;
		$result = mysql_query($req);
		while ($row = mysql_fetch_array($result))
		{
			$this->tabActor[$this->nbElement] = new ACTOR();
			$this->tabActor[$this->nbElement]->setId($row['IDA']);
			$this->tabActor[$this->nbElement]->setNom($row['FIRSTNAME']);
			$this->tabActor[$this->nbElement]->setPrenom($row['LASTNAME']);
			$this->nbElement++;
		}
	}

	public	function	next()
	{
		$this->currentPos++;
	}
	public	function	insertActor()
	{
		$this->currentPos = 0;
		while ($this->isCurrentElementExist())
		{
			$req = 'SELECT count(*) FROM ACTOR WHERE FIRSTNAME = "' . $this->tabActor[$this->currentPos]->getNom() .'" AND LASTNAME = "' . 	$this->tabActor[$this->currentPos]->getPrenom() . '"';
			$result = mysql_query($req);
			$row = mysql_fetch_array($result);
			if ($row['count(*)'] == 0)
			{
				$req = 'INSERT into ACTOR(FIRSTNAME,LASTNAME) VALUES("' . $this->tabActor[$this->currentPos]->getNom() .'","' . $this->tabActor[$this->currentPos]->getPrenom() . '")';
				$result = mysql_query($req);
			}
			$req = 'SELECT IDA FROM ACTOR WHERE FIRSTNAME = "' . $this->tabActor[$this->currentPos]->getNom() .'" AND LASTNAME = "' . 	$this->tabActor[$this->currentPos]->getPrenom() . '"';
			$result = mysql_query($req);
			$row = mysql_fetch_array($result);
			$this->tabActor[$this->currentPos]->setId($row['IDA']);
			$this->next();
		}
		$this->currentPos = 0;
	}
	public	function	insertJouer($idM)
	{
		$this->currentPos = 0;
		while ($this->isCurrentElementExist())
		{
			$req = 'INSERT into PLAY(IDM,IDA) VAlUES(' . $idM . ',' . $this->getElement()->getId() . ')';
			$result = mysql_query($req);
			$this->next();
		}
		$this->currentPos = 0;
	}

	public	function	addElementbyTab($tab)
	{
		$tab = explode(',', $tab);
		foreach ($tab as $val)
		{
			$this->tabActor[$this->nbElement] = new ACTOR();
			$val = explode(' ', trim($val));
			$this->tabActor[$this->nbElement]->setNom(trim($val[1]));
			$this->tabActor[$this->nbElement]->setPrenom(trim($val[0]));
			$this->nbElement++;
		}
	}

	public	function	isCurrentElementExist()
	{
		return isset($this->tabActor[$this->currentPos]);
	}


	public	function	getElement()
	{
		return $this->tabActor[$this->currentPos];
	}

	public	function	getTabActor()
	{
		return $this->tabActor;
	}
	public	function	getnbretabElement()
	{
		return $this->nbElement;
	}
}
?>